package com.citycloud.ccuap.tc.admin.projectrelapp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.citycloud.ccuap.framework.service.mybatis.MybatisServiceImpl;
import com.citycloud.ccuap.tc.admin.entity.SysProjectRelApp;
import com.citycloud.ccuap.tc.admin.repository.SysProjectRelAppRepository;

@Service
public class SysProjectRelAppServiceImpl extends MybatisServiceImpl<SysProjectRelAppRepository, SysProjectRelApp, Long> implements SysProjectRelAppService {
    @Autowired
    private SysProjectRelAppRepository sysProjectRelAppRepository;

    @Override
    public SysProjectRelApp getSysProjectRelAppByProjectId(Long objId) {
        List<SysProjectRelApp> sysProjectRelApps = sysProjectRelAppRepository.findByProjectIdOrderByObjIdDesc(objId);
        if (CollectionUtils.isEmpty(sysProjectRelApps)) {
            return null;
        }
        return sysProjectRelApps.get(0);
    }


    @Override
    public void saveOrUpdate(SysProjectRelApp dto) {
        SysProjectRelApp sysProjectRelApp=new SysProjectRelApp();
        BeanUtils.copyProperties(dto,sysProjectRelApp);
        if(dto.getObjId()!=null){
            sysProjectRelAppRepository.updateIgnoreNull(sysProjectRelApp);
            return;
        }
        sysProjectRelAppRepository.insert(sysProjectRelApp);
    }

    @Override
    public int deleteByProjectId(Long objId) {
       return sysProjectRelAppRepository.deleteByProjectId(objId);
    }

    @Override
    public List<SysProjectRelApp> findByProjectTypeId(String projectTypeId) {
        Map<String,Object> param = new HashMap<>(4);
        param.put("sql","select app.* from sys_project_rel_app app left join sys_project p on app.project_id = p.project_id where p.deleted != true and app.project_type_id = #{projectTypeId} ;");
        param.put("projectTypeId",projectTypeId);
        return (List<SysProjectRelApp> ) selectListBySimple(param);
    }
}
